
module("luci.controller.nac.nac", package.seeall)

local utl = require "luci.util"
local uci = require "luci.model.uci".cursor()
local http = require "luci.http"
local dkjson = require "dkjson"

function index()
	local uci = require "luci.model.uci".cursor()
	local has_switch = false
        local is_local = false
        local is_auth = false
        local mobile_enable = false

	if not nixio.fs.access("/etc/config/nac") then
		return 
	end
	if not nixio.fs.access("/etc/config/devmode") then
		has_switch = true
	else
		uci:foreach("devmode", "device",function(s)
			if s.mode == "nac" or s.mode == "ac+nac" then
				has_switch = true
				return false
			end
		end)
	end
        uci:foreach("nac", "radius", function(s)
                if s.auth_type == "2" then
                        is_local = true
                        return false
                end
        end)
        uci:foreach("nac", "portal", function(s)
                if s.flag == "0" then
                        is_auth = true
                        return false
                end
        end)
        uci:foreach("network","interface",function(s)
            if s.proto == "3g" then
                mobile_enable = true
                return false
            end
            -- body
        end)

function upload()
    -- body

    entry({"admin", "ac_nac", "upload"}, cbi("nac/upload") )
end


uci:foreach("network", "interface",function(f)
    if f.ifname ~= nil then

            if (string.sub(f.ifname,1,4)) == "eth0"  and f.type ~= "bridge" then
                
                wan_enable = true
                return false
    
            end
    end
end)



	entry({"admin", "ac_nac"}, firstchild(), _("AC/NAC"), 60)
        if mobile_enable and wan_enable then
            entry({"admin","ac_nac","wan_backup"},cbi("nac/wan_backup"),_("WAN Backup policy"),21)
        end
	if has_switch then
		-- NAC 配置
		entry({"admin", "ac_nac", "nac"}, cbi("nac/nac"), _("NAC configuration"), 20).index = true
    
		-- NAC在线用户管理
		entry({"admin", "ac_nac", "onlineuser"}, cbi("nac/onlineuser") , _("NAC online users"), 22)
		entry({"admin", "ac_nac", "onlineuser_drop"}, call("onlineuser_drop"), nil).leaf = true
		 entry({"admin", "ac_nac", "onlineuser", "onlineuser_display"}, call("onlineuser_display"), nil).leaf = true
		if is_local and is_auth then
			--NAC本地用户管理
			entry({"admin","ac_nac","localuser"}, arcombine(cbi("nac/localuser"),cbi("nac/localuser_detail")), _("NAC user management"), 23).leaf = true
			entry({"admin", "ac_nac", "localuser_delete"}, call("localuser_delete"), nil).leaf = true
		end
	end 

end

--[[
    @brief: json编码，将table转换为json
]]--
function json_encode( obj )
    local str = dkjson.encode(obj, {indent = false})
    return str
end

-- 获取在线用户
function onlineuser_display( ... )
        local start = http.formvalue("iDisplayStart") or ""
        local count = http.formvalue("iDisplayLength") or ""
        local sEcho = http.formvalue("sEcho") or ""

        local ubusrt = utl.ubus("nac_proxy", "websrv", {act='getonlineuser',data={offset=start, limit=count, sEcho=sEcho}}) or {msg="ubus error",rt_code="0"}
        local rt_code = ubusrt["rt_code"] or ""
        local msg = ubusrt["msg"] or {}
      -- 将数据转json
      local buff = json_encode(msg)
      luci.http.prepare_content("application/json")
      luci.http.write(buff)
end

-- 强制某个用户下线
function onlineuser_drop(name)
    local tb_info = {}
    tb_info["name"] = name
    tb_info["usermac"] = luci.http.formvalue("usermac") or ""
    tb_info["userip"] = luci.http.formvalue("userip") or ""
    result = utl.ubus("nac_proxy", "websrv", {act='dropuser',data=tb_info}) or {}
    luci.http.redirect(luci.dispatcher.build_url("admin/ac_nac/onlineuser") .. "?code=" .. result.rt_code .. "&msg_code=" .. (result.msg.code or result.rt_code))
end

-- 删除本地用户
function localuser_delete(user_name)
 	local tb = {}
 	tb["ver"] = "1"
 	tb["user_name"] = user_name

 	result = utl.ubus("nac_proxy", "websrv", {act='deluser', data=tb}) or {}
 	 luci.http.redirect(luci.dispatcher.build_url("admin/ac_nac/localuser") .. "?code=" .. result.rt_code .. "&msg_code=" .. (result.msg.code or result.rt_code))
 end 


